/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.bookingmanager.web;

import cz.muni.fi.pa165.bookingmanager.adapters.BookingAdapter;
import cz.muni.fi.pa165.bookingmanager.dto.CustomerDTO;
import cz.muni.fi.pa165.bookingmanager.dto.HotelDTO;
import cz.muni.fi.pa165.bookingmanager.dto.RoomDTO;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Daniel Sak
 */
@UrlBinding("/booking/${event}")
public class BookingActionBean extends MyActionBean {

    final static Logger log = LoggerFactory.getLogger(BookingActionBean.class);
    private BookingAdapter bookingAdapter;
    private List<CustomerDTO> list = new ArrayList<CustomerDTO>();
    private Date date;
    private int duration;

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public Date getDate() {
        if(date == null){
            return null;
        }
        
        return new Date(date.getTime());
    }

    public void setDate(Date date) {
        this.date = new Date(date.getTime());
    }

    public List<CustomerDTO> getList() {
        if (this.list == null) {
            return null;
        }
        return Collections.unmodifiableList(list);
    }

    public void setList(List<CustomerDTO> list) {
        this.list = new ArrayList<CustomerDTO>();
        this.list.addAll(list);
    }

    @DefaultHandler
    public Resolution all() {
        log.debug("all()");
        list = bookingAdapter.findAllCurrentCustomersInHotel(getContext().getHotel(), new java.sql.Date(System.currentTimeMillis()), 1);
        return new ForwardResolution("/backend/hotels/booking/list.jsp");
    }

    public BookingActionBean() throws NamingException {
        Context ctx = new InitialContext();
        this.bookingAdapter = (BookingAdapter) ctx.lookup("java:global/cz.muni.fi.pa165.bookingmanager_booking_manager-web_war_1.0-SNAPSHOT/BookingAdapterImpl!cz.muni.fi.pa165.bookingmanager.adapters.BookingAdapter");
    }

    public Resolution findAllFreeRooms() {
        return new ForwardResolution("backend/hotels/booking/list.jsp");
    }

    public List<RoomDTO> findAllFreeRooms(HotelDTO hotel, Date startTime, int duration) {
        return bookingAdapter.findAllFreeRooms(hotel, startTime, duration);
    }

    public Resolution findAllCurrentCustomersInHotel() {
        if (date != null && duration > 0) {
            list = bookingAdapter.findAllCurrentCustomersInHotel(getContext().getHotel(), date, duration);
            return new ForwardResolution("/backend/hotels/booking/list.jsp");
        } else {
            return new RedirectResolution(this.getClass(), "all");
        }
    }

    public List<CustomerDTO> findAllCurrentCustomersInHotel(HotelDTO hotel, Date startTime, int duration) {
        return bookingAdapter.findAllCurrentCustomersInHotel(hotel, startTime, duration);
    }

    public Resolution searchHotelByConditions() {
        return new ForwardResolution("backend/booking/list.jsp");
    }

    public List<HotelDTO> searchHotelByConditions(String city, BigDecimal maxPrice, int minCapacity) {
        return bookingAdapter.searchHotelByConditions(city, maxPrice, minCapacity);
    }

    public Resolution searchRoomInHotel() {
        return new ForwardResolution("backend/booking/list.jsp");
    }

    public List<RoomDTO> searchRoomInHotel(HotelDTO hotel, int capacity, BigDecimal maxPrice) {
        return bookingAdapter.searchRoomInHotel(hotel, capacity, maxPrice);
    }
}
